package com.frxuexi.park2212.patrol

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityPointDetailBinding
import com.frxuexi.park2212.repair.RepairAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/PointDetailActivity")
@AndroidEntryPoint
class PointDetailActivity : BaseActivity<ActivityPointDetailBinding, PatrolViewModel>() {
    @Autowired
    lateinit var pid: String
    val standList = listOf<String>(
        "1 绿地是否需要修剪",
        "2 现场是否有垃圾",
        "3 是否有安全隐患",
        "4 设施是否有损坏",
    )
    val standAdapter by lazy { PonitStandAdapter() }
    val taskAdapter by lazy { TaskAdapter() }
    val repairAdapter by lazy { RepairAdapter() }
    override fun initData() {
        vm.getPointById.observe(this) {
            binding.bean = it
            binding.pointdetailStatus.apply {
                if (it.patrolType == "保洁巡检") {
                    setBackgroundResource(R.drawable.shape_solid_blue)
                } else if (it.patrolType == "安防巡检") {
                    setBackgroundResource(R.drawable.shape_solid_darkorange)
                } else {
                    setBackgroundResource(R.drawable.shape_solid_darkgreen)
                }
            }
        }

        vm.getTasks.observe(this) {
            taskAdapter.addData(it)
            taskAdapter.notifyDataSetChanged()
        }
        vm.getOrders.observe(this){
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getPointsById(pid.toInt())

        binding.headPointdetail.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("点位详情")
            headerAdd.visibility = View.GONE
        }

        binding.pointdetailTab.apply {
            addTab(newTab().setText("巡检标准"))
            addTab(newTab().setText("巡检记录"))
            addTab(newTab().setText("工单记录"))
            addOnTabSelectedListener(object : OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    when (tab?.position) {
                        0 ->
                            binding.rvPointdetail.apply {
                                adapter = standAdapter
                                addItemDecoration(
                                    DividerItemDecoration(
                                        context,
                                        DividerItemDecoration.VERTICAL // 或 HORIZONTAL
                                    )
                                )
                            }
                        1 ->
                            binding.rvPointdetail.apply {
                                adapter = taskAdapter
                            }

                        2 ->
                            binding.rvPointdetail.adapter=repairAdapter
                    }

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
        }

        standAdapter.addData(standList)
        binding.rvPointdetail.apply {
            adapter = standAdapter
            layoutManager = LinearLayoutManager(this@PointDetailActivity)
        }

        vm.getTasks()
        vm.getOrders()

    }

}